package cn.e3mall.controller;

import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.common.utils.ValueUtil;
import cn.e3mall.pojo.TbItemDesc;
import cn.e3mall.manager.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e3mall.common.pojo.EasyUIDataGridResult;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.manager.service.ItemService;

import java.util.List;
import java.util.Map;

/**
 * 商品管理Controller
 */
@Controller
@ResponseBody
@RequestMapping(value = "/item")
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemCatService itemCatService;

    @RequestMapping("/{itemId}")
    public TbItem getItemById(@PathVariable Long itemId) {
        TbItem tbItem = itemService.getItemById( itemId );
        return tbItem;
    }

    @RequestMapping("/list")
    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
        //调用服务查询商品列表
        EasyUIDataGridResult result = itemService.getItemList( page, rows );
        return result;
    }

    @RequestMapping("/save")
    public String saveItem(@RequestParam Map<String, String> map) {
//		System.out.println(map);
        itemService.save( map );
        return ValueUtil.toJson( HttpStatus.SC_OK, "新增商品成功" );
    }

    @RequestMapping("/desc/{itemId}")
    public String findDesc(@PathVariable Long itemId) {
        TbItemDesc desc = itemService.findDesc( itemId );
        return ValueUtil.toJson( HttpStatus.SC_OK, desc );
    }

    @RequestMapping("/cat/list")
    public List<EasyUITreeNode> getItemList(@RequestParam(name = "id", defaultValue = "0") Long parentId) {
        //调用服务查询商品列表
        List<EasyUITreeNode> result = itemCatService.getItemCat( parentId );
        return result;
    }


}
